package com.zmt.mrms.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class VerifyUtils {

	public String getAgeForBirthday(String birthday) {
		// 定义一个日期格式yyyy-MM-dd，将String转为Date
		SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = simp.parse(birthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// 判断该生日是否在当前日期之前,设置一个初始值，表示输入日期错误
		if (date.after(new Date())) {
			return "";
		}
		// 获取当前日历对象中的年、月、日
		Calendar nowc = Calendar.getInstance();
		int nowYear = nowc.get(Calendar.YEAR);
		int nowMonth = nowc.get(Calendar.MONTH);
		int nowDay = nowc.get(Calendar.DAY_OF_MONTH);
		// 将Date转为Calendar日历对象,获取生日的年、月、日
		nowc.setTime(date);
		// 通过年月日计算该对象的年纪
		// 先通过Year计算初步年龄
		int year = nowYear - nowc.get(Calendar.YEAR);

		// 通过Month和Day判断是否过生日

		if (nowc.get(Calendar.MONTH) > nowMonth) {
			return Integer.toString(year - 1);
		}
		if (nowc.get(Calendar.DAY_OF_MONTH) > nowDay) {
			return Integer.toString(year - 1);
		}
		return Integer.toString(year);
	}

	public Boolean isValidIDCard(String idcard) {
		if (idcard.length() == 0) {
			return true;
		}
		return IdCardVerification.isValidatedAllIdcard(idcard);
	}

	public boolean isValidPwd(String pwd) {
		// 密码必须包含大写字母、小写字母、数字3种类型的8位-20位的组合
		String pwdrule = "(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]).{8,20}";
		return Pattern.matches(pwdrule, pwd);
	}

	public boolean isValidEmail(String email) {
		String rule = "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$";
		if ((email != null) && (!email.isEmpty())) {
			return Pattern.matches(rule, email);
		}
		return false;
	}

}
